import unittest
import utils
from api.login import LoginAPI
from api.employee import EmployeeAPI


class TestEmployee(unittest.TestCase):
    TOKEN = None
    staff_id = None

    def setUp(self) -> None:
        self.login_api = LoginAPI()
        self.employee_api = EmployeeAPI()

    def tearDown(self) -> None:
        pass

    def test01_login(self):
        response = self.login_api.login({"mobile": "13800000002", "password": "123456"})
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(True, response.json().get("success"))
        self.assertEqual(10000, response.json().get("code"))
        self.assertIn("操作成功", response.json().get("message"))

        TestEmployee.TOKEN = "Bearer " + response.json().get("data")
        # print(TestEmployee.TOKEN)

    def test02_insert_employee(self):
        insert_data = {
            "username": "番茄011000",
            "mobile": "18762666005",
            "timeOfEntry": "2020-07-09",
            "formOfEmployment": 1,
            "workNumber": "1878605290005",
            "departmentName": "销售",
            "departmentId": "1266699057968001024",
            "correctionTime": "2020-07-30T16:00:00.000Z"
        }
        response = self.employee_api.insert_staff(insert_data=insert_data, token=TestEmployee.TOKEN)
        print(response.json())
        # self.assertEqual(200, response.status_code)
        # self.assertEqual(True, response.json().get("success"))
        # self.assertEqual(10000, response.json().get("code"))
        # self.assertIn("操作成功", response.json().get("message"))
        utils.common_assert(self, response)
        # 提取id 保存到类的属性中
        TestEmployee.staff_id = response.json().get("data").get("id")
        print(TestEmployee.staff_id)

    def test03_select_employee(self):
        response = self.employee_api.select_staff(TestEmployee.staff_id, TestEmployee.TOKEN)
        print(response.json())
        utils.common_assert(self, response)

    def test04_update_empolyee(self):
        response = self.employee_api.update_staff(TestEmployee.staff_id, {"username": "番茄011001"}, TestEmployee.TOKEN)
        print(response.json())
        utils.common_assert(self, response)

    def test05_select_employee(self):
        response = self.employee_api.select_staff(TestEmployee.staff_id, TestEmployee.TOKEN)
        print(response.json())
        utils.common_assert(self, response)

    def test06_delete_employee(self):
        response = self.employee_api.delete_staff(TestEmployee.staff_id, TestEmployee.TOKEN)
        print(response.json())
        utils.common_assert(self, response)
